*===============================================================================
* This code runs placebo reduced form regressions to further test stringency.
*===============================================================================

********************************************************************************
* IV and OLS Analysis for LEHD *
********************************************************************************

use "${build}/iv_fin_lehd_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1
cap drop if acs_sample == 1

keep if ageatcase >=18 & ageatcase<=55 

*Create datasets we will populate with results 
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount

**************************
*Regression specification*
**************************
global cluster_ols="judgexyear"
global cluster_iv="judgexyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtxyearxquarter"

*Outcomes
local outcomes_main = "black hispanic noprior joint ad_damnum fem nbrhd_pov_rate_5yr nbrhd_med_rent_5yr earn_b8to1 earn_b12to9"


***********************
* OLS/ IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.

foreach pop in "all"   {
	foreach popval of numlist 1  {
		if "`pop'"~="all" | `popval'~=0	{	
			qui  foreach var of varlist `outcomes_main'  {

				nois di " ON outcome: `var', pop: `pop'=`popval' "
				
				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'  i.$fe  (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
				
				local label="Pop: `pop'=`popval', Outcome: `var'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (.) (.) (.)  (.) (.)
				
				*RF: Baseline
				eststo clear 
				eststo: reg `var' $instrument  i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Specification: RF Baseline" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
			
				noi esttab , p keep(loo*)
					
				}
			}
		}
	}


frame results: export delimited using "${build}/stringency_robustness_LEHD.csv", replace 


********************************************************************************
* IV and OLS Analysis for MARF *
********************************************************************************

use "${build}/iv_fin_marf_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1
cap drop if acs_sample == 1

keep if ageatcase >=18 & ageatcase<=55 

*Create datasets we will populate with results
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount


**************************
*Regression specification*
**************************
global cluster_ols="judgeXyear"
global cluster_iv="judgeXyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtXyearXquarter"

*Outcomes
local outcomes_main = "not_at_evicn4_to_n3  avg_pov_raten4_to_n3 avg_median_rentn4_to_n3  not_at_evicn2_to_n1 avg_pov_raten2_to_n1 avg_median_rentn2_to_n1  "


***********************
* OLS/ IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.

foreach pop in "all"   {
	foreach popval of numlist 1  {
		if "`pop'"~="all" | `popval'~=0	{	
			qui  foreach var of varlist `outcomes_main'  {

				nois di " ON outcome: `var', pop: `pop'=`popval' "
				
				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'  i.$fe  (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
				
				local label="Pop: `pop'=`popval', Outcome: `var'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (.) (.) (.)  (.) (.)
				
				
				*RF: Baseline
				eststo clear 
				eststo: reg `var' $instrument  i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Specification: RF Baseline" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
			
				noi esttab , p keep(loo*)
					
				}
			}
		}
	}


frame results: export delimited using "${build}/stringency_robustness_MARF.csv", replace 


********************************************************************************
* IV and OLS Analysis for HMIS *
********************************************************************************
clear

use "${build}/iv_fin_hmis_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1
cap drop if acs_sample == 1

keep if ageatcase >=18 & ageatcase<=55 

*Create datasets we will populate with results
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount

**************************
*Regression specification*
**************************
global cluster_ols="judgeXyear"
global cluster_iv="judgeXyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtXyearXquarter"

*Outcomes
local outcomes_main = "n_shelter1_pre  n_em_shelter1_pre "


***********************
* OLS/ IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.

foreach pop in "all"   {
	foreach popval of numlist 1  {
		if "`pop'"~="all" | `popval'~=0	{	
			  qui foreach var of varlist `outcomes_main'  {

				nois di " ON outcome: `var', pop: `pop'=`popval' "
				
				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'  i.$fe  (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
			
				local label="Pop: `pop'=`popval', Outcome: `var'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (.) (.) (.)  (.) (.)
				
				
				*RF: Baseline
				eststo clear 
				eststo: reg `var' $instrument  i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Specification: RF Baseline" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
			
				noi esttab , p keep(loo*)
					
			}
		}
	}
}

frame results: export delimited using "${build}/stringency_robustness_HMIS.csv", replace 


import delimited "${build}/stringency_robustness_LEHD.csv", clear 
tempfile sr 
save `sr'

import delimited "${build}/stringency_robustness_MARF.csv", clear 
tempfile sr2 
save `sr2'

import delimited "${build}/stringency_robustness_HMIS.csv", clear 
append using `sr'
append using `sr2'

	gen specification = "RF Baseline"
	drop if mi(D) | regexm(D, "Lagged|to")
	drop B
	
	rename (D E) (t_21 t_43)
	reshape long t , i(A C) j(time_period) string
	replace time_period = "-2 to -1" if time_period=="_21"
	replace time_period = "-4 to -3" if time_period=="_43"
	
	replace C = "Non_Evicted_Mean" if C=="Mean Outcome for Non Evicted"
	replace C = "Non_Evicted_Std_Dev" if C=="Std Dev Outcome Non Evicted"
	replace C = "Observations" if C=="Number of Observations"
	replace C = subinstr(C, " ", "_", .)
	replace C = subinstr(C, "Std_Err", "SE", .)

	destring t, replace
	reshape wide t, i(A specification time_period) j(C) string
	rename t* *
	rename ime_period time_period
	rename A outcome


export delimited "${Inputs}\Cook_judgestringency_regs_ivols.csv", replace
